package com.zero.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.logistics.pojo.MessageObject;
import com.zero.logistics.pojo.BaseData;
import com.zero.logistics.pojo.BaseDataExample;
import com.zero.logistics.pojo.Customer;
import com.zero.logistics.pojo.CustomerExample;
import com.zero.logistics.pojo.CustomerExample.Criteria;
import com.zero.logistics.pojo.User;
import com.zero.logistics.pojo.UserExample;
import com.zero.logistics.service.BaseDataService;
import com.zero.logistics.service.CustomerService;
import com.zero.logistics.service.UserService;
import com.zero.logistics.utils.Constant;

/*
 *	@date:2020年3月31日 上午5:00:13
 *	@author:Zero
 *	@version:Future
 *	@description: 
 *
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {
	
	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private BaseDataService baseDataService;

	@RequiresPermissions("customer:customerPage")
	@RequestMapping("/customerPage")
	public String customerPage() {
		return "customerPage";
	}
	
	/**
	 * @function 分页显示客户
	 * @param pageNum
	 * @param pageSize
	 * @param keywords
	 * @return
	 */
	@RequiresPermissions("customer:list")
	@RequestMapping("list")
	@ResponseBody  //返回json类型的数据
	public PageInfo<Customer> customerList(@RequestParam(defaultValue = "1")int pageNum,
			@RequestParam(defaultValue = "10")int pageSize,
			String keywords){
		
		PageHelper.startPage(pageNum, pageSize);
		CustomerExample example = new CustomerExample();
		Criteria criteria = example.createCriteria();
		// 1.设计逻辑：当前登录的用户是业务员的时候，该用户只能查看自己的客户
		// 1.1 通过安全管理器工具获取到当前的主体
		Subject subject = SecurityUtils.getSubject();
		User user = (User)subject.getPrincipal();
		// 1.2 判断主体的角色是否为业务员，如果是业务员就只能查看自己的客户
		if ("业务员".equals(user.getRolename())) {
			criteria.andUserIdEqualTo(user.getUserId());
		}
		
		// 2.定义搜索的逻辑（使用管理）
		if (StringUtils.isNotBlank(keywords)) {  //当前台传过来的关键字不等于空的时候
			//根据客户名查询
			Criteria criteria1 = example.createCriteria();
			criteria1.andCustomerNameLike("%" + keywords + "%");
		}
		
		List<Customer> customers = customerService.selectByExample(example);
		PageInfo<Customer> pageInfo = new PageInfo<>(customers);
		
		return pageInfo;
	}
	
	/**
	 * @function 删除客户
	 * @param customerId
	 * @return
	 */
	@RequiresPermissions("customer:delete")
	@RequestMapping("/delete")
	@ResponseBody
	public MessageObject customerDelete(Long customerId) {
		
		//1.根据客户id删除客户
		int row = customerService.deleteByPrimaryKey(customerId);
		MessageObject mo = MessageObject.createMo(0, "删除数据失败，请联系技术人员");
		if (row == 1) {
			mo = MessageObject.createMo(1, "删除数据成功");
		}
		return mo;
	}
	
	/**
	 * @function 批量删除(以String类型接收前端传过来的参数)
	 * @param map
	 * @return
	 */
	@RequiresPermissions("customer:batchdel")
	@RequestMapping("/batchdel")
	@ResponseBody
	public MessageObject customerBatchdel(String customerIds) {
		System.out.println(customerIds);
		String[] customerIdsArr = customerIds.split(",");
		
		//1.根据客户id的数组批量删除客户
		int rows = customerService.deleteByIds(customerIdsArr);
		MessageObject mo = MessageObject.createMo(0, "批量删除数据失败，请联系技术人员");
		if (rows != 0) {
			mo = MessageObject.createMo(1, "批量删除数据成功");
		}
		return mo;
	}
	
	/**
	 * @function 提供编辑页面(在新增客户和更新客户的时候都需要执行此方法)
	 * @param m
	 * @return
	 */
	@RequestMapping("/edit")
	public String customerEdit(Model m, Long customerId) {	
		//1.当前端传过来的customerId不为空的时候，就通过customerId查询客户信息并共享数据，用于更新的情况
		if (customerId != null) {
			Customer customer = customerService.selectByPrimaryKey(customerId);
			System.out.println("需要更新的客户信息：" + customer.getCustomerId());
			m.addAttribute("customer", customer);
		}
		
		//2.查询出所有业务员的信息，并共享给编辑页面
		UserExample example1 = new UserExample();
		com.zero.logistics.pojo.UserExample.Criteria criteria1 = example1.createCriteria();
		criteria1.andRolenameLike("%" + Constant.ROLE_SALESMAN + "%");
		List<User> clerks = userService.selectByExample(example1);
		m.addAttribute("clerks", clerks);
		
		//3.查询出所有区间的基础数据，并共享给编辑页面
		BaseDataExample example2 = new BaseDataExample();
		com.zero.logistics.pojo.BaseDataExample.Criteria criteria2 = example2.createCriteria();
		criteria2.andParentNameLike("%" + Constant.BASE_DATA_REGION + "%");
		List<BaseData> regions = baseDataService.selectByExample(example2);
		m.addAttribute("regions", regions);

		return "customerEdit";
	}
	
	/**
	 * @function 新增客户
	 * @param customer
	 * @return
	 */
	@RequiresPermissions("customer:insert")
	@RequestMapping("/insert")
	@ResponseBody
	public MessageObject customerInsert(Customer customer) {
		// 1.插入数据并返回结果
		int row = customerService.insert(customer);
		MessageObject mo = MessageObject.createMo(0, "添加数据失败，请联系技术人员");
		if (row == 1) {
			mo = MessageObject.createMo(1, "添加数据成功");
		}
		return mo;
	}
	
	/**
	 * @function 更新客户信息
	 * @param customer
	 * @return
	 */
	@RequiresPermissions("customer:update")
	@RequestMapping("update")
	@ResponseBody
	public MessageObject customerUpdate(Customer customer) {
		// 1.更新数据并返回结果(这里要用动态sql语句)
		int row = customerService.updateByPrimaryKeySelective(customer);
		MessageObject mo = MessageObject.createMo(0, "更新数据失败，请联系技术人员");
		if (row == 1) {
			mo = MessageObject.createMo(1, "更新数据成功");
		}
		return mo;
	}

}
